{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "04233.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/04233.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OookItZvO_Q0",
        "colab_type": "text"
      },
      "source": [
        "## 3.7 softmax 回归的简洁实现"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7DSmjESDBYMK",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 134
        },
        "outputId": "999b51f9-df90-4f18-8197-5fac80a11b35"
      },
      "source": [
        "!git clone https://github.com/ShusenTang/Dive-into-DL-PyTorch.git"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'Dive-into-DL-PyTorch'...\n",
            "remote: Enumerating objects: 84, done.\u001b[K\n",
            "remote: Counting objects:   1% (1/84)\u001b[K\rremote: Counting objects:   2% (2/84)\u001b[K\rremote: Counting objects:   3% (3/84)\u001b[K\rremote: Counting objects:   4% (4/84)\u001b[K\rremote: Counting objects:   5% (5/84)\u001b[K\rremote: Counting objects:   7% (6/84)\u001b[K\rremote: Counting objects:   8% (7/84)\u001b[K\rremote: Counting objects:   9% (8/84)\u001b[K\rremote: Counting objects:  10% (9/84)\u001b[K\rremote: Counting objects:  11% (10/84)\u001b[K\rremote: Counting objects:  13% (11/84)\u001b[K\rremote: Counting objects:  14% (12/84)\u001b[K\rremote: Counting objects:  15% (13/84)\u001b[K\rremote: Counting objects:  16% (14/84)\u001b[K\rremote: Counting objects:  17% (15/84)\u001b[K\rremote: Counting objects:  19% (16/84)\u001b[K\rremote: Counting objects:  20% (17/84)\u001b[K\rremote: Counting objects:  21% (18/84)\u001b[K\rremote: Counting objects:  22% (19/84)\u001b[K\rremote: Counting objects:  23% (20/84)\u001b[K\rremote: Counting objects:  25% (21/84)\u001b[K\rremote: Counting objects:  26% (22/84)\u001b[K\rremote: Counting objects:  27% (23/84)\u001b[K\rremote: Counting objects:  28% (24/84)\u001b[K\rremote: Counting objects:  29% (25/84)\u001b[K\rremote: Counting objects:  30% (26/84)\u001b[K\rremote: Counting objects:  32% (27/84)\u001b[K\rremote: Counting objects:  33% (28/84)\u001b[K\rremote: Counting objects:  34% (29/84)\u001b[K\rremote: Counting objects:  35% (30/84)\u001b[K\rremote: Counting objects:  36% (31/84)\u001b[K\rremote: Counting objects:  38% (32/84)\u001b[K\rremote: Counting objects:  39% (33/84)\u001b[K\rremote: Counting objects:  40% (34/84)\u001b[K\rremote: Counting objects:  41% (35/84)\u001b[K\rremote: Counting objects:  42% (36/84)\u001b[K\rremote: Counting objects:  44% (37/84)\u001b[K\rremote: Counting objects:  45% (38/84)\u001b[K\rremote: Counting objects:  46% (39/84)\u001b[K\rremote: Counting objects:  47% (40/84)\u001b[K\rremote: Counting objects:  48% (41/84)\u001b[K\rremote: Counting objects:  50% (42/84)\u001b[K\rremote: Counting objects:  51% (43/84)\u001b[K\rremote: Counting objects:  52% (44/84)\u001b[K\rremote: Counting objects:  53% (45/84)\u001b[K\rremote: Counting objects:  54% (46/84)\u001b[K\rremote: Counting objects:  55% (47/84)\u001b[K\rremote: Counting objects:  57% (48/84)\u001b[K\rremote: Counting objects:  58% (49/84)\u001b[K\rremote: Counting objects:  59% (50/84)\u001b[K\rremote: Counting objects:  60% (51/84)\u001b[K\rremote: Counting objects:  61% (52/84)\u001b[K\rremote: Counting objects:  63% (53/84)\u001b[K\rremote: Counting objects:  64% (54/84)\u001b[K\rremote: Counting objects:  65% (55/84)\u001b[K\rremote: Counting objects:  66% (56/84)\u001b[K\rremote: Counting objects:  67% (57/84)\u001b[K\rremote: Counting objects:  69% (58/84)\u001b[K\rremote: Counting objects:  70% (59/84)\u001b[K\rremote: Counting objects:  71% (60/84)\u001b[K\rremote: Counting objects:  72% (61/84)\u001b[K\rremote: Counting objects:  73% (62/84)\u001b[K\rremote: Counting objects:  75% (63/84)\u001b[K\rremote: Counting objects:  76% (64/84)\u001b[K\rremote: Counting objects:  77% (65/84)\u001b[K\rremote: Counting objects:  78% (66/84)\u001b[K\rremote: Counting objects:  79% (67/84)\u001b[K\rremote: Counting objects:  80% (68/84)\u001b[K\rremote: Counting objects:  82% (69/84)\u001b[K\rremote: Counting objects:  83% (70/84)\u001b[K\rremote: Counting objects:  84% (71/84)\u001b[K\rremote: Counting objects:  85% (72/84)\u001b[K\rremote: Counting objects:  86% (73/84)\u001b[K\rremote: Counting objects:  88% (74/84)\u001b[K\rremote: Counting objects:  89% (75/84)\u001b[K\rremote: Counting objects:  90% (76/84)\u001b[K\rremote: Counting objects:  91% (77/84)\u001b[K\rremote: Counting objects:  92% (78/84)\u001b[K\rremote: Counting objects:  94% (79/84)\u001b[K\rremote: Counting objects:  95% (80/84)\u001b[K\rremote: Counting objects:  96% (81/84)\u001b[K\rremote: Counting objects:  97% (82/84)\u001b[K\rremote: Counting objects:  98% (83/84)\u001b[K\rremote: Counting objects: 100% (84/84)\u001b[K\rremote: Counting objects: 100% (84/84), done.\u001b[K\n",
            "remote: Compressing objects:   1% (1/69)\u001b[K\rremote: Compressing objects:   2% (2/69)\u001b[K\rremote: Compressing objects:   4% (3/69)\u001b[K\rremote: Compressing objects:   5% (4/69)\u001b[K\rremote: Compressing objects:   7% (5/69)\u001b[K\rremote: Compressing objects:   8% (6/69)\u001b[K\rremote: Compressing objects:  10% (7/69)\u001b[K\rremote: Compressing objects:  11% (8/69)\u001b[K\rremote: Compressing objects:  13% (9/69)\u001b[K\rremote: Compressing objects:  14% (10/69)\u001b[K\rremote: Compressing objects:  15% (11/69)\u001b[K\rremote: Compressing objects:  17% (12/69)\u001b[K\rremote: Compressing objects:  18% (13/69)\u001b[K\rremote: Compressing objects:  20% (14/69)\u001b[K\rremote: Compressing objects:  21% (15/69)\u001b[K\rremote: Compressing objects:  23% (16/69)\u001b[K\rremote: Compressing objects:  24% (17/69)\u001b[K\rremote: Compressing objects:  26% (18/69)\u001b[K\rremote: Compressing objects:  27% (19/69)\u001b[K\rremote: Compressing objects:  28% (20/69)\u001b[K\rremote: Compressing objects:  30% (21/69)\u001b[K\rremote: Compressing objects:  31% (22/69)\u001b[K\rremote: Compressing objects:  33% (23/69)\u001b[K\rremote: Compressing objects:  34% (24/69)\u001b[K\rremote: Compressing objects:  36% (25/69)\u001b[K\rremote: Compressing objects:  37% (26/69)\u001b[K\rremote: Compressing objects:  39% (27/69)\u001b[K\rremote: Compressing objects:  40% (28/69)\u001b[K\rremote: Compressing objects:  42% (29/69)\u001b[K\rremote: Compressing objects:  43% (30/69)\u001b[K\rremote: Compressing objects:  44% (31/69)\u001b[K\rremote: Compressing objects:  46% (32/69)\u001b[K\rremote: Compressing objects:  47% (33/69)\u001b[K\rremote: Compressing objects:  49% (34/69)\u001b[K\rremote: Compressing objects:  50% (35/69)\u001b[K\rremote: Compressing objects:  52% (36/69)\u001b[K\rremote: Compressing objects:  53% (37/69)\u001b[K\rremote: Compressing objects:  55% (38/69)\u001b[K\rremote: Compressing objects:  56% (39/69)\u001b[K\rremote: Compressing objects:  57% (40/69)\u001b[K\rremote: Compressing objects:  59% (41/69)\u001b[K\rremote: Compressing objects:  60% (42/69)\u001b[K\rremote: Compressing objects:  62% (43/69)\u001b[K\rremote: Compressing objects:  63% (44/69)\u001b[K\rremote: Compressing objects:  65% (45/69)\u001b[K\rremote: Compressing objects:  66% (46/69)\u001b[K\rremote: Compressing objects:  68% (47/69)\u001b[K\rremote: Compressing objects:  69% (48/69)\u001b[K\rremote: Compressing objects:  71% (49/69)\u001b[K\rremote: Compressing objects:  72% (50/69)\u001b[K\rremote: Compressing objects:  73% (51/69)\u001b[K\rremote: Compressing objects:  75% (52/69)\u001b[K\rremote: Compressing objects:  76% (53/69)\u001b[K\rremote: Compressing objects:  78% (54/69)\u001b[K\rremote: Compressing objects:  79% (55/69)\u001b[K\rremote: Compressing objects:  81% (56/69)\u001b[K\rremote: Compressing objects:  82% (57/69)\u001b[K\rremote: Compressing objects:  84% (58/69)\u001b[K\rremote: Compressing objects:  85% (59/69)\u001b[K\rremote: Compressing objects:  86% (60/69)\u001b[K\rremote: Compressing objects:  88% (61/69)\u001b[K\rremote: Compressing objects:  89% (62/69)\u001b[K\rremote: Compressing objects:  91% (63/69)\u001b[K\rremote: Compressing objects:  92% (64/69)\u001b[K\rremote: Compressing objects:  94% (65/69)\u001b[K\rremote: Compressing objects:  95% (66/69)\u001b[K\rremote: Compressing objects:  97% (67/69)\u001b[K\rremote: Compressing objects:  98% (68/69)\u001b[K\rremote: Compressing objects: 100% (69/69)\u001b[K\rremote: Compressing objects: 100% (69/69), done.\u001b[K\n",
            "Receiving objects:   0% (1/1682)   \rReceiving objects:   1% (17/1682)   \rReceiving objects:   2% (34/1682)   \rReceiving objects:   3% (51/1682)   \rReceiving objects:   4% (68/1682)   \rReceiving objects:   5% (85/1682)   \rReceiving objects:   6% (101/1682)   \rReceiving objects:   7% (118/1682)   \rReceiving objects:   8% (135/1682)   \rReceiving objects:   9% (152/1682)   \rReceiving objects:  10% (169/1682)   \rReceiving objects:  11% (186/1682)   \rReceiving objects:  12% (202/1682)   \rReceiving objects:  13% (219/1682)   \rReceiving objects:  14% (236/1682)   \rReceiving objects:  15% (253/1682)   \rReceiving objects:  16% (270/1682)   \rReceiving objects:  17% (286/1682)   \rReceiving objects:  18% (303/1682)   \rReceiving objects:  19% (320/1682)   \rReceiving objects:  20% (337/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  21% (354/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  22% (371/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  23% (387/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  24% (404/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  25% (421/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  26% (438/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  27% (455/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  28% (471/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  29% (488/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  30% (505/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  31% (522/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  32% (539/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  33% (556/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  34% (572/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  35% (589/1682), 1.57 MiB | 3.12 MiB/s   \rReceiving objects:  35% (592/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  36% (606/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  37% (623/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  38% (640/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  39% (656/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  40% (673/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  41% (690/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  42% (707/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  43% (724/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  44% (741/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  45% (757/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  46% (774/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  47% (791/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  48% (808/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  49% (825/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  50% (841/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  51% (858/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  52% (875/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  53% (892/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  54% (909/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  55% (926/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  56% (942/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  57% (959/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  58% (976/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  59% (993/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  60% (1010/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  61% (1027/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  62% (1043/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  63% (1060/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  64% (1077/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  65% (1094/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  66% (1111/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  67% (1127/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  68% (1144/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  69% (1161/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  70% (1178/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  71% (1195/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  72% (1212/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  73% (1228/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  74% (1245/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  75% (1262/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  76% (1279/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  77% (1296/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  78% (1312/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  79% (1329/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  80% (1346/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  81% (1363/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  82% (1380/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  83% (1397/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  84% (1413/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  85% (1430/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  86% (1447/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  87% (1464/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  88% (1481/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  89% (1497/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  90% (1514/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  91% (1531/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  92% (1548/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  93% (1565/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  94% (1582/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  95% (1598/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  96% (1615/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  97% (1632/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects:  98% (1649/1682), 13.19 MiB | 12.75 MiB/s   \rremote: Total 1682 (delta 34), reused 40 (delta 15), pack-reused 1598\u001b[K\n",
            "Receiving objects:  99% (1666/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects: 100% (1682/1682), 13.19 MiB | 12.75 MiB/s   \rReceiving objects: 100% (1682/1682), 25.35 MiB | 17.62 MiB/s, done.\n",
            "Resolving deltas:   0% (0/944)   \rResolving deltas:   1% (12/944)   \rResolving deltas:   2% (22/944)   \rResolving deltas:   3% (30/944)   \rResolving deltas:   4% (46/944)   \rResolving deltas:   6% (59/944)   \rResolving deltas:   7% (68/944)   \rResolving deltas:   8% (77/944)   \rResolving deltas:   9% (86/944)   \rResolving deltas:  11% (110/944)   \rResolving deltas:  12% (115/944)   \rResolving deltas:  13% (124/944)   \rResolving deltas:  14% (135/944)   \rResolving deltas:  15% (142/944)   \rResolving deltas:  16% (153/944)   \rResolving deltas:  17% (162/944)   \rResolving deltas:  18% (170/944)   \rResolving deltas:  19% (185/944)   \rResolving deltas:  20% (189/944)   \rResolving deltas:  21% (199/944)   \rResolving deltas:  22% (210/944)   \rResolving deltas:  23% (219/944)   \rResolving deltas:  24% (227/944)   \rResolving deltas:  26% (248/944)   \rResolving deltas:  27% (255/944)   \rResolving deltas:  30% (289/944)   \rResolving deltas:  31% (302/944)   \rResolving deltas:  32% (303/944)   \rResolving deltas:  33% (313/944)   \rResolving deltas:  34% (321/944)   \rResolving deltas:  35% (331/944)   \rResolving deltas:  36% (342/944)   \rResolving deltas:  37% (351/944)   \rResolving deltas:  38% (361/944)   \rResolving deltas:  43% (407/944)   \rResolving deltas:  44% (417/944)   \rResolving deltas:  45% (425/944)   \rResolving deltas:  46% (441/944)   \rResolving deltas:  47% (444/944)   \rResolving deltas:  48% (458/944)   \rResolving deltas:  49% (469/944)   \rResolving deltas:  50% (472/944)   \rResolving deltas:  52% (499/944)   \rResolving deltas:  57% (544/944)   \rResolving deltas:  58% (552/944)   \rResolving deltas:  59% (563/944)   \rResolving deltas:  60% (567/944)   \rResolving deltas:  61% (577/944)   \rResolving deltas:  62% (586/944)   \rResolving deltas:  63% (600/944)   \rResolving deltas:  64% (606/944)   \rResolving deltas:  70% (670/944)   \rResolving deltas:  71% (677/944)   \rResolving deltas:  72% (681/944)   \rResolving deltas:  73% (690/944)   \rResolving deltas:  74% (701/944)   \rResolving deltas:  75% (708/944)   \rResolving deltas:  76% (722/944)   \rResolving deltas:  78% (742/944)   \rResolving deltas:  79% (746/944)   \rResolving deltas:  80% (756/944)   \rResolving deltas:  81% (768/944)   \rResolving deltas:  83% (791/944)   \rResolving deltas:  84% (798/944)   \rResolving deltas:  86% (817/944)   \rResolving deltas:  87% (827/944)   \rResolving deltas:  88% (831/944)   \rResolving deltas:  90% (850/944)   \rResolving deltas:  91% (862/944)   \rResolving deltas:  92% (869/944)   \rResolving deltas:  94% (893/944)   \rResolving deltas:  95% (897/944)   \rResolving deltas:  96% (907/944)   \rResolving deltas:  97% (923/944)   \rResolving deltas:  98% (928/944)   \rResolving deltas:  99% (940/944)   \rResolving deltas: 100% (944/944)   \rResolving deltas: 100% (944/944), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-5Q8xrGZP3YU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "from torch import nn \n",
        "from torch.nn import init \n",
        "import numpy as np \n",
        "import sys \n",
        "sys.path.append('Dive-into-DL-PyTorch/code')\n",
        "import d2lzh_pytorch as d2l"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SDYs6lHVBjfW",
        "colab_type": "text"
      },
      "source": [
        "### 3.7.1 获取和读取数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fA28zTaNBp7m",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 272
        },
        "outputId": "5d2827b8-5693-4b7e-8833-7d2a1d8b6fbc"
      },
      "source": [
        "batch_size = 256\n",
        "train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "  0%|          | 0/26421880 [00:00<?, ?it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw/train-images-idx3-ubyte.gz\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "26427392it [00:00, 65396885.21it/s]                              \n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Extracting /root/Datasets/FashionMNIST/FashionMNIST/raw/train-images-idx3-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "32768it [00:00, 453210.99it/s]\n",
            "  1%|          | 40960/4422102 [00:00<00:12, 359845.86it/s]"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw/train-labels-idx1-ubyte.gz\n",
            "Extracting /root/Datasets/FashionMNIST/FashionMNIST/raw/train-labels-idx1-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw\n",
            "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw/t10k-images-idx3-ubyte.gz\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "4423680it [00:00, 21292128.33it/s]                         \n",
            "8192it [00:00, 153519.16it/s]\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Extracting /root/Datasets/FashionMNIST/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw\n",
            "Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz\n",
            "Extracting /root/Datasets/FashionMNIST/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to /root/Datasets/FashionMNIST/FashionMNIST/raw\n",
            "Processing...\n",
            "Done!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3urTJqXrCYn0",
        "colab_type": "text"
      },
      "source": [
        "### 3.7.2 定义和初始化模型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4w435AYiC67y",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "num_inputs = 784 \n",
        "num_outputs = 10 \n",
        "\n",
        "class LinearNet(nn.Module):\n",
        "    def __init__(self, num_inputs, num_outputs):\n",
        "        super(LinearNet, self).__init__()\n",
        "        self.linear = nn.Linear(num_inputs, num_outputs)\n",
        "    def forward(self, x):\n",
        "        y = self.linear(x.view(x.shape[0], -1))\n",
        "        return y \n",
        "\n",
        "net = LinearNet(num_inputs, num_outputs)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y3OgrnttDshw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class FlattenLayer(nn.Module):\n",
        "    def __init__(self):\n",
        "        super(FlattenLayer, self).__init__()\n",
        "    def forward(self, x):\n",
        "        return x.view(x.shape[0], -1)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SkDD2_yzEJt_",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from collections import OrderedDict \n",
        "\n",
        "net = nn.Sequential(\n",
        "    OrderedDict([\n",
        "        ('flatten', FlattenLayer()), \n",
        "        ('linear', nn.Linear(num_inputs, num_outputs))\n",
        "    ])\n",
        ")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9dfknuUDEn1M",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "init.normal_(net.linear.weight, mean=0, std=0.01)\n",
        "init.constant_(net.linear.bias, val=0);"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AuVarS2oE0af",
        "colab_type": "text"
      },
      "source": [
        "### 3.7.3 softmax 和交叉熵函数"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "y1g2_nVxJzQ5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "loss = nn.CrossEntropyLoss()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mJmp4u0CJ4Dz",
        "colab_type": "text"
      },
      "source": [
        "### 3.7.4 定义优化算法"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0Pp-4NKUJ_P0",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "optimizer = torch.optim.SGD(net.parameters(), lr=0.1)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rignhBJSKIC8",
        "colab_type": "text"
      },
      "source": [
        "### 3.7.5 训练模型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "e3rswDTzKMLZ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "ab4961e6-5ffe-465d-f11e-80043ca6aaa1"
      },
      "source": [
        "num_epochs = 5 \n",
        "d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "epoch 1, loss 0.0031, train acc 0.752, test acc 0.782\n",
            "epoch 2, loss 0.0022, train acc 0.814, test acc 0.810\n",
            "epoch 3, loss 0.0021, train acc 0.826, test acc 0.820\n",
            "epoch 4, loss 0.0020, train acc 0.831, test acc 0.817\n",
            "epoch 5, loss 0.0019, train acc 0.837, test acc 0.829\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}